package com.scsj.supermarket.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.scsj.supermarket.R;
import com.scsj.supermarket.bean.GoodsSortBean;
import com.vondear.rxtools.RxDeviceTool;

import java.util.ArrayList;
import java.util.List;

/**
 * 店铺详情-积分换购
 */
public class ShopPointsAdapter extends RecyclerView.Adapter<ShopPointsAdapter.MyViewHolder> {
    private LayoutInflater inflater;
    private Context mContext;
    public static int GRID = 0;//排列方式 0 网格
    public static int LINEAR = 1;//排列方式  1 列表
    private int arrayType = 0;//排列方式 0 网格 1 列表
    private List<GoodsSortBean> mDatas = new ArrayList<>();
    private double imgRatio = 1;//图片比例 = 高/宽

    public interface OnItemClickListener {
        void onItemClick(View view, GoodsSortBean data, int postion);

    }

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }

    private OnItemClickListener mOnItemClickListener;

    public ShopPointsAdapter(Context context) {
        this.mContext = context;
        inflater = LayoutInflater.from(context);
    }

    public void addAllData(List<GoodsSortBean> mDatas) {
        this.mDatas.addAll(mDatas);
        notifyDataSetChanged();
    }

    public void clearData() {
        this.mDatas.clear();
        notifyDataSetChanged();
    }

    /**
     * 点击切换布局的时候调用这个方法设置type
     *
     * @param arrayType 商品排列的方式 0：网格；1：垂直列表排列
     */
    public void setArrayType(int arrayType) {
        this.arrayType = arrayType;
    }

    @Override
    public int getItemViewType(int position) {
        return arrayType;
    }

    @Override
    public ShopPointsAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        MyViewHolder myViewHolder;
        if (viewType == LINEAR) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_shop_points_vertical, parent, false);
            myViewHolder = new MyViewHolder(view);
        } else {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_shop_points_grid, parent, false);
            myViewHolder = new MyViewHolder(view);
            ViewGroup.LayoutParams params = myViewHolder.img_iv.getLayoutParams();
            DisplayMetrics dm = RxDeviceTool.getDisplayMetrics(mContext);
            int width = (dm.widthPixels - 6) / 2;
//        int width = dm.widthPixels / 2;
            params.height = (int) (width * imgRatio);
            params.width = width;
            myViewHolder.img_iv.setLayoutParams(params);
        }

        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        GoodsSortBean itemBean = mDatas.get(position);
//        holder.title_tv.setText(itemBean.getTitle());

        setItemEventClick(holder);
    }


    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return this.mDatas.size();
    }


    protected void setItemEventClick(final MyViewHolder myViewHolder) {
        if (mOnItemClickListener != null) {
            myViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int layoutPostion = myViewHolder.getLayoutPosition();
                    GoodsSortBean data = mDatas.get(layoutPostion);
                    mOnItemClickListener.onItemClick(view, data, layoutPostion);
                }
            });
        }
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        ImageView img_iv;
        TextView title_tv;
        TextView price_yuan_tv, price_coin_tv;
        TextView comm_num_tv, comm_good_tv;

        public MyViewHolder(View itemView) {
            super(itemView);
            img_iv = itemView.findViewById(R.id.img_iv);
            title_tv = itemView.findViewById(R.id.title_tv);
            price_yuan_tv = itemView.findViewById(R.id.price_yuan_tv);
            price_coin_tv = itemView.findViewById(R.id.price_coin_tv);
            comm_num_tv = itemView.findViewById(R.id.comm_num_tv);
            comm_good_tv = itemView.findViewById(R.id.comm_good_tv);
        }
    }

}
